
<template>
  <div class="drawerBox">
    <el-drawer v-model="drawerData.open" v-if="drawerData.open">
      <template #header>
        <div style="width: 100%">{{ drawerData.title }}</div>
      </template>
      <template #default>


         
        <el-form
          :model="form"
          label-width="120px"
          :rules="formRules"
          ref="forms"
          class="formBox"
        >
          <el-form-item label="专题名称：" prop="title">
            <el-input
              v-model="form.title"
              placeholder="请输入专题名称"
              allow-caear
            />
          </el-form-item>
          <el-form-item label="评语图片：" class="typeUpload" prop="file">

             <el-upload
              v-model:file-list="file"
    ref="upload"
    class="upload-demo"
 
    :limit="1"
    :auto-upload='false'
                 :action="uploadAction"
                :show-file-list="true"
                :on-change="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
  >
    <template #trigger>
      <el-button   class="mainBtn">
        上传文件
      </el-button>
    </template>
   

  </el-upload>
       
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <div style="flex: auto">
          <el-button
            class="secondBtn"
            @click="resetForm(forms), (drawerData.open = false)"
            >返回</el-button
          >
          <el-button class="mainBtn" @click="submitForm(forms)">保存</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>


<script  setup>
import { ref, watch, defineEmits } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { topicSave } from "../../../api/index";
const emit = defineEmits(["getLists"]);

const drawerData = ref({
  open: false,
  title: "",
});

//校验规则
const formRules = ref({
  title: [
    {
      required: true,
      message: "请输入标题",
      trigger: "blur",
    },
  ],

  file: [{ required: true, message: "请上传文件", trigger: "change" }],
});
const forms = ref(null);
let form = ref({
  title: "",
  file: [],
});

watch(
  drawerData.value,
  (newValue, oldValue) => {
    if (drawerData.value.open == true && forms.value) {
      resetForm(forms.value);
    }
  },
  { deep: true, immediate: true }
);

// 图片

const handleAvatarSuccess = (response, uploadFile) => {
  console.log(response, uploadFile[0]);
  form.value.file = response.raw;
  // console.log(response.raw);
};
const beforeAvatarUpload = (rawFile) => {
  // console.log(rawFile);

  // if (rawFile.type !== "image/jpeg" && rawFile.type !== "image/png") {
  //   ElMessage.error("支持jpeg，png!");
  //   return false;
  // } else if (rawFile.size / 1024 / 1024 > 2) {
  //   ElMessage.error("最大 2MB!");
  //   return false;
  // }

  // return true;
};

//表单保存
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      let data = new FormData();
      data.append("file", form.value.file);

      data.append("title", form.value.title);
      topicSave(data).then((res) => {
        if (res.code == 200) {
          emit("getLists", res);
          ElMessage.success("保存成功");
          drawerData.value.open = false;
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

defineExpose({ drawerData, form });
</script>


<style lang="scss" scoped>
.drawerBox {
  :deep(.el-drawer .el-drawer__body) {
    padding: 16px;
    .el-form-item {
      .el-form-item__label {
        font-size: 14px;
        font-family: MicrosoftYaHei;
        color: #999999;
        padding: 0px;
        margin-right: 8px;
      }
      .el-form-item__content {
        .el-input {
          width: 280px;
        }
      }
    }
  }
}

// 上传
// .typeUpload :deep(.el-form-item__content) {
//   flex-wrap: inherit !important;
//   align-items: flex-end !important;

//   div:first-child {
//     margin-right: 8px;

//     .el-upload--picture-card {
//       background-color: #ffffff;
//     }
//   }

//   .imgtext {
//     font-size: 14px;
//     font-family: MicrosoftYaHei;
//     color: #999999;
//     line-height: 24px;
//   }
//   .avatar {
//     width: 146px;
//     height: 146px;
//     border: 1px dashed transparent;
//   }

//   .el-upload {
//     border: 1px dashed #ebebeb;
//     border-radius: 6px;
//     width: 146px;
//     height: 146px;
//     display: flex;
//     justify-content: center;
//     align-items: center;
//   }
// }

// 单选
:deep(.el-radio-group) {
  .el-radio__label {
    font-size: 14px;
    font-family: MicrosoftYaHei;
    color: #333333 !important;
  }
  .el-radio__input.is-checked .el-radio__inner {
    border-color: #3370ff;
    background: #3370ff;
  }
}

// 多选
:deep(.el-checkbox-group) {
  .el-checkbox__input.is-checked .el-checkbox__inner {
    border-color: #3370ff;
    background: #3370ff;
  }
  .el-checkbox__label {
    font-size: 14px;
    font-family: MicrosoftYaHei;
    color: #333333 !important;
  }
}

.demo-image__lazy {
  height: 400px;
  overflow-y: auto;
}
.demo-image__lazy .el-image {
  display: block;
  min-height: 200px;
  margin-bottom: 10px;
}
.demo-image__lazy .el-image:last-child {
  margin-bottom: 0;
}
.title {
  font-size: 16px;
  font-family: MicrosoftYaHei-Bold, MicrosoftYaHei;
  font-weight: bold;
  color: #333333;
  margin-bottom: 16px;
  padding-top: 16px;
}
:deep(.el-upload-list__item){
  width: 350px;
}
</style>



